<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import = "java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Update</title>
</head>
<body style="font-family: arial">
<%
String name = null;
String s = null;
String s0 = null;
String s1 = null;
boolean flag = false;

if(null != request.getParameter("form"))
{
	if (null != request.getParameter("name") && " " != request.getParameter("name"))
	{
		try
		{
			//Loading the driver
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

			// Getting Connection
			Connection con = DriverManager.getConnection("jdbc:odbc:jdbctest", "admin", "admin");
			
			PreparedStatement st;
			
			String t0 = request.getParameter("name");
			String t1 = request.getParameter("address");
			String t2 = request.getParameter("phone");
			String t4 = request.getParameter("hiddentext");
			
			st = con.prepareStatement("update student set name=?, address=?, phone=? where name=?");
			st.setString(1, t0);
			st.setString(2, t1);
			st.setString(3, t2);
			st.setString(4, t4);
			int ex = st.executeUpdate();
			System.out.print("update success!");

			st.close();
			con.close();
			response.sendRedirect("http://localhost:8080/ex21/Ex22.jsp");
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	else response.sendRedirect("http://localhost:8080/ex21/Ex22.jsp");
}
else if(null != request.getParameter("id"))
{

name = request.getParameter("id");
name.trim();

try{
	/** Loading the driver*/
	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

	/** Getting Connection*/
	Connection con = DriverManager.getConnection("jdbc:odbc:jdbctest", "admin", "admin");
	
	/** Creating Statement*/
	Statement stmt = con.createStatement();
	
	name.trim();
	String query = "select * from student ";
	System.out.println(query);
	ResultSet rs=stmt.executeQuery(query);

	while(rs.next()) {
		s = rs.getString(1);
		s0 = rs.getString(2);
		s1 = rs.getString(3);
		s.trim();
		if (name.equalsIgnoreCase(s))
		{
			flag = true;
			break;
		}
	}

	/** Closing the Connection*/
	stmt.close();
	con.close();
}
catch (Exception e)
{
	e.printStackTrace();
}
finally
{
%>

<% if(flag){ %>
<form method=POST name="form">
<b>Name:</b> <input type="text" name="name" value=<% out.print("\"" + s + "\""); %>/><br/><b>Address:</b> <input type="text" name="address" value=<% out.print("\"" + s0 + "\""); %>/><br/><b>Phone:</b> <input type="text" name="phone" value=<% out.print("\"" + s1 + "\""); %>/>
<input type="hidden" name="hiddentext" value=<% out.print("\"" + s + "\""); %>/><br/><input type="submit" name="form"/><input type="reset"/>
</form>
<a href="http://localhost:8080/ex21/Ex22.jsp">Back</a>
<%}else response.sendRedirect("http://localhost:8080/ex21/Ex22.jsp");

}}
else response.sendRedirect("http://localhost:8080/ex21/Ex22.jsp");

%>
</body>
</html>